package com.jasmine.bjstkserver.manager.weather;

import com.jasmine.core.constants.enums.DictEnum;
import com.jasmine.bjstkserver.server.dict.DictEntity;
import com.jasmine.bjstkserver.server.dict.DictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jasmineXz
 */
@RestController
@RequestMapping("/scheduled/weather")
public class HeWeatherScheduled {
    private static final Logger log = LoggerFactory.getLogger(HeWeatherScheduled.class);

    @Autowired
    private WeatherManager weatherManager;

    @Autowired
    private DictService dictService;

    /**
     * 30分钟刷新
     */
    @RequestMapping
    @Scheduled(cron="0 */10 * * * ?")
    public void refreshWeather () {
        log.info("[BJSTK] 刷新天气");

        List<DictEntity> dicts = dictService.findList(DictEnum.WEATHER_HE.codeInt());
        Map<String,List<DictEntity>> locations =
                dicts.stream()
                    .filter(p -> "LOCATION".equals(p.getDictCname()))
                    .collect(Collectors.groupingBy(DictEntity::getDictValue));

        locations.forEach((k,v) -> {
            weatherManager.clearAll(k,null);
            weatherManager.findWeatherAll(k,null);
        });
    }

}
